<?php

namespace app\serversms\service\sms;
use app\manage\service\ApiBase;
use \app\serversms\interfaces\SmsInterface;

/**
 * @author:      李颖
 * @DateTime:    2017-06-26 15:34:20
 * @Description: 云片短信服务
 */

class YunpianSms extends ApiBase implements SmsInterface
{
    //获得账户
    function get_user($ch,$apikey){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
        return curl_exec($ch);
    }
    function send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }
    function batchSend($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/batch_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }
    function tpl_send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }
    function voice_send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }

    //发送单条信息
    function sendOneSms($data){

        header("Content-Type:text/mobile;charset=utf-8");
        $smsData['apikey'] = $data['send_info']['apikey']; 
        $smsData['text']   = $data['send_info']['text'];
        $smsData['mobile'] = $data['send_info']['mobile']; 
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));       /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);   /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_POST, 1);  /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        
        $json_data = $this->send($ch,$smsData); // 发送短信
        return json_decode($json_data,true);
    } 

    /*批量发送相同内容*/
    function sendBatchSms($data){

        header("Content-Type:text/mobile;charset=utf-8");
        $smsData['apikey'] = $data['send_info']['apikey']; 
        $smsData['text']   = $data['send_info']['text'];
        $smsData['mobile'] = $data['send_info']['mobile'];
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));       /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);   /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_POST, 1);  /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        
        $json_data = $this->batchSend($ch,$smsData); // 发送短信
        return json_decode($json_data,true);

    }
}



?>